package cn.edu.tsc.controller;

import cn.edu.tsc.dao.EmpDao;
import cn.edu.tsc.pojo.Emp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/UpdateEmpServlet")
public class UpdateEmpServlet extends HttpServlet {
    public UpdateEmpServlet() {
        super();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        EmpDao empDao = new EmpDao();
        Emp emp = new Emp(req.getParameter("name"),
                req.getParameter("depart"),
                Integer.parseInt(req.getParameter("salary")));

        emp.setId(Integer.parseInt(req.getParameter("id")));

        int flag = empDao.updateEmp(emp);
        if (flag > 0)
            req.getRequestDispatcher("FindAllServlet").forward(req, resp);

        else
            resp.sendRedirect("error.jsp");


    }
}
